;void recortar_asm (
;	unsigned char *src,  	rdi 
;	unsigned char *dst,	rsi
;	int m,			rdx
;	int n,			rcx
;	int src_row_size,	r8
;	int dst_row_size,	r9
;	int x,			[rsp + 0]
;	int y,			[rsp + 8]
;	int tam			[rsp + 16]
;);

global recortar_asm

recortar_asm:
	push rbp
	mov rbp, rsp
	push rbx
	push r12
	push r13
	push r14
	push r15
	
	mov ebx, [rbp + 16]	; ebx: primera columna
	xor rax, rax
	mov eax, [rbp + 24]	; eax: primera fila
	
	imul rax, r8 		; eax: desplazamiento
	lea rdi, [rdi + rax]	; acomodo fila
	lea rdi, [rdi + rbx]	; acomodo columna	

	mov r13, rdi		; r13: puntero origen
	mov r12, rsi		; r12: puntero destino
	
	mov ecx, [rbp + 32] 	; rcx: tam
	mov edx, ecx		; edx: tam
	mov ebx, ecx		; ebx: tam
	shr ecx, 4		; rcx: tam / 16
				; indice por iteracion de 16 bits	

	mover_fila:
	movdqu xmm1, [r13]
	movdqu [r12], xmm1
	lea r13, [r13 +16]
	lea r12, [r12 +16]
	sub rcx, 1
	cmp rcx, 0
	ja mover_fila

	;falta copiar los ultimos bytes de la fila?
	mov ecx, ebx
	and ecx, 15
	cmp ecx, 0
	je seguir

	copiar_byte:
	mov al, [r13]
	mov [r12], al
	inc r13
	inc r12
	dec ecx
	cmp ecx, 0
	ja copiar_byte	

	seguir:
	lea rdi, [rdi + r8]
	mov r13, rdi
	lea rsi, [rsi + r9]
	mov r12, rsi
	mov ecx, ebx
	shr ecx, 4
	dec edx
	cmp edx, 0
	ja mover_fila
	
	pop r15
	pop r14
	pop r13
	pop r12
	pop rbx
	pop rbp

	ret
